package com.example.springboot.controller;

import com.alibaba.excel.EasyExcel;
import com.example.springboot.domain.PageBean;
import com.example.springboot.domain.User;

import com.example.springboot.services.UserService;
import com.example.springboot.vo.ResVO;
import com.example.springboot.vo.SerchVO;
import lombok.Data;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@Controller
@RequestMapping("/test")
public class MybatisController {

    @Autowired
    private UserService userService;

    //删除数据
    @DeleteMapping(value = "/delUserById")
    @ResponseBody
    public ResVO<Integer> delUserById(@RequestParam Long id) {
        if(id!=null) {
            Boolean result = userService.delUserById(id);
            if(result) return new ResVO<>(3001, "删除成功", null);
            return new ResVO<>(3002, "删除失败", null);
        }
        return new ResVO<>(3003, "数据异常", null);
    }
//分页访问数据,返回分页数据和数据总数



//更新用户数据
    @PostMapping(value = "/searchUser")
    @ResponseBody
    public ResVO<PageBean<User>> searchUser(@RequestBody SerchVO serchVO) {
        //对请求数据进行操作
        Integer page=(serchVO.getPageNum()-1)*serchVO.getPageSize();
        String username=serchVO.getUsername().trim();
        String name=serchVO.getName().trim();
        String sex=serchVO.getSex().trim();

        //访问service
        PageBean<User> userPage=userService.pageQuery(page,serchVO.getPageSize(),username,name,sex);

        //响应请求数据
        return new ResVO<>(1001, "查询成功", userPage);
    }



    //添加用户
    @PostMapping(value = "/addUser")
    @ResponseBody
    public ResVO<Integer> addUser(@RequestBody User user) {
        //判断请求用户名是否为空
        if(user.getUsername()==null){
            //用户名为空，返回失败信息
            return new ResVO<>(2003, "数据异常", null);
        }

        //查找相同用户名
        Boolean result=userService.addUser(user);
        //存在相同用户名，返回相同信息
        if(result){
            return new ResVO<>(2001, "添加成功", null);
        }
        return new ResVO<>(2002, "用户名已存在", null);
    }

    //修改用户
    @PostMapping(value = "/updateUser")
    @ResponseBody
    public ResVO<Integer> updateUser(@RequestBody User user) {
        //判断请求是否为空
        if(user.getId()==null){
                //返回失败信息
                return new ResVO<>(4003, "数据异常", null);
            }

        Boolean result = userService.updateUser(user);
        if(!result){
            return new ResVO<>(4002, "用户名已存在", null);
        }
        return new ResVO<>(4001, "修改成功", null);


    }

    //通过id获取用户信息用户
    @GetMapping(value = "/findUserById/")
    @ResponseBody
    public ResVO<User> findUserById(HttpServletRequest request)  {
        Long id = Long.parseLong(request.getParameter("params"));

        User user=userService.findUserById(id);

        //返回查找结果
        return new ResVO<>(1001, "查询成功", user);
    }

    @PostMapping("download")
    @ResponseBody
    public void download(HttpServletResponse response,@RequestBody SerchVO serchVO) throws IOException {

        //对请求数据进行操作
        String username=serchVO.getUsername().trim();
        String name=serchVO.getName().trim();
        String sex=serchVO.getSex().trim();
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

        //获取用户数据
        PageBean<User> userPage = userService.pageQuery(0,0,username,name,sex);

        EasyExcel.write(response.getOutputStream(), User.class).sheet("模板").doWrite(userPage.getData());
    }

    @PostMapping("download2")
    @ResponseBody
    public void download2(HttpServletResponse response, @RequestBody List<Long> idlist) throws IOException {

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");

        //获取用户数据
        List<User> users = userService.findUserByIdList(idlist);

        EasyExcel.write(response.getOutputStream(), User.class).sheet("模板").doWrite(users);
    }


}
